package com.zerosoft.shuguang.serial;

import com.zerosoft.shuguang.dao.GuotaiEventParse;
import com.zerosoft.shuguang.service.MqttSendClient;
import com.zerosoft.shuguang.service.MqttSerialClient;
import jakarta.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class SerialPortCallback {

//    @Resource
//    private MqttSerialClient mqttSerialClient;
    private MqttSendClient mqttSendClient;

    final private int BUF_LEN = 125;

    public SerialPortCallback(MqttSendClient client)
    {
        this.mqttSendClient = client;
//        this.mqttSerialClient = new MqttSerialClient();
    }

    public void dataAvailable() {
        try {
            //当前监听器监听到的串口返回数据 back
            byte[] back = SerialPortManager.readSerialPortData();
//            processData(back);
//            back[back.length-1]=0;
            String data = new String(back);
            System.out.println("back-"+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))+"--"+back.length+">>"+data);
            int start=0;
            while(start<back.length)
            {
                byte[] segment = new byte[BUF_LEN];
                System.arraycopy(back, start, segment, 0, Math.min(BUF_LEN, back.length-start));
                start+=125;
                processData(segment);
            }
////            String s = ConvertHexStrAndStrUtils.bytesToHexStr(back);
////            System.out.println("rev--data:"+s);
//            String realData = "";
//            System.out.println("data:--"+data.length()+":"+data.substring(0, 2));
//            if(back.length>=BUF_LEN && data.substring(0, 2).equals("$$"))
//            {
//                GuotaiEventParse guotaiEventParse = new GuotaiEventParse();
//                int dataLength = (int)data.substring(2, 3).getBytes()[0];
//                System.out.println("dataLength:"+dataLength);
//                realData = data.substring(3, dataLength-1);
////                while(realData.length()<BUF_LEN-4){
////                    realData += " ";
////                }
//                System.out.println("realData:"+realData);
//                guotaiEventParse.setDeviceType((int)realData.substring(0, 1).getBytes()[0]);
//                guotaiEventParse.setEventType((int)realData.substring(1, 2).getBytes()[0]);
//                guotaiEventParse.setDate(realData.substring(2, 10));
//                guotaiEventParse.setTime(realData.substring(10, 18));
//                guotaiEventParse.setBuild(realData.substring(18, 32));
//                guotaiEventParse.setMachine(realData.substring(32, 36));
//                guotaiEventParse.setEventNumber(realData.substring(36, 50));
//                guotaiEventParse.setPanelTypeName(realData.substring(50, 62));
//                guotaiEventParse.setRoad(realData.substring(62, 69));
//                guotaiEventParse.setPartTypeName(realData.substring(69, 81));
//                guotaiEventParse.setDescription(realData.substring(81, 105));
//                guotaiEventParse.setPosition(realData.substring(105, 121));
//
////                mqttSendClient.connect();
//                System.out.println(guotaiEventParse.parseJson());
//                mqttSendClient.publish(true,"event/guotai",guotaiEventParse.parseJson());
//
//                //TODO: 保存数据库
////                        FireAlarm fireAlarm = new FireAlarm(guotaiEventParse);
////                        fireAlarmService.addFireAlarm(fireAlarm);
//
//                //回写$$
////                SerialPortManager.sendSerialPortData("$$".getBytes());
////                if(data.length()>dataLength){
////                    data = data.substring(dataLength, data.length());
////                }
////                else{
////                    data = "";
////                }
////                System.out.println("data:"+data);
//
//            }
        //throw new Exception();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public void processData(byte[] data) throws Exception{
        if(data.length>=3 && data[0]-(int)('$') ==0 && data[1]-(int)('$')==0)
        {
            GuotaiEventParse guotaiEventParse = new GuotaiEventParse();
            int dataLength = (int)data[2];
            System.out.println("dataLength:"+dataLength+"==bufLen:"+data.length);
            guotaiEventParse.setDeviceType((int)data[3]);
            guotaiEventParse.setEventType((int)data[4]);
            guotaiEventParse.setDate(new String(data, 5, 8).trim());
            guotaiEventParse.setTime(new String(data, 13, 8).trim());
            guotaiEventParse.setBuild(new String(data, 21, 14, "GB2312").trim());
            guotaiEventParse.setMachine(new String(data, 35, 4, "GB2312").trim());
            guotaiEventParse.setEventNumber(new String(data, 39, 14, "GB2312").trim());
            guotaiEventParse.setPanelTypeName(new String(data, 53, 12, "GB2312").trim());
            guotaiEventParse.setRoad(data.length>=65? new String(data, 65, Math.min(7,data.length-65), "GB2312").trim():"");
            guotaiEventParse.setPartTypeName(data.length>=72?new String(data, 72, Math.min(12,data.length-72), "GB2312").trim():"");
            guotaiEventParse.setDescription(data.length>=84?new String(data, 84, Math.min(24,data.length-84), "GB2312").trim():"");
            guotaiEventParse.setPosition(data.length>=108?new String(data, 108, Math.min(16,data.length-108), "GB2312").trim():"");

//                mqttSendClient.connect();
            System.out.println(guotaiEventParse.parseJson());
            mqttSendClient.publish(true,"event/guotai",guotaiEventParse.parseJson());

            //TODO: 保存数据库
//                        FireAlarm fireAlarm = new FireAlarm(guotaiEventParse);
//                        fireAlarmService.addFireAlarm(fireAlarm);

            //回写$$
//                SerialPortManager.sendSerialPortData("$$".getBytes());


        }

    }
}
